package com.example.test.httpclient;


import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class HttpClient {
    private static String URL="http://127.0.0.1:8080/hello";
    private static String URL1="http://127.0.0.1:8080/httpClientGet";
    private static String URL2="http://127.0.0.1:8080/httpClientPost";


    public static void main(String[] args) throws IOException, URISyntaxException {
        doGet(URL1,null);

    }
    public static void doGet(String url, Map<String,Object> map) throws IOException, URISyntaxException {
        //1.获取连接客户端工具
        CloseableHttpClient client= HttpClients.createDefault();
        //2.创建url
        URIBuilder uriBuilder=new URIBuilder("http://127.0.0.1:8080/httpClientGet");
        //3.添加参数 2种方法添加参数
          // 第一种添加参数方法
         // uriBuilder.addParameter("name","小明");
        //uriBuilder.addParameter("age","22");
        //第二种添加参数方法
        List<NameValuePair> list=new LinkedList<>();
        BasicNameValuePair param1=new BasicNameValuePair("name","大白");
        BasicNameValuePair param2=new BasicNameValuePair("age","23");
        list.add(param1);
        list.add(param2);
        uriBuilder.setParameters(list);
        // 4.创建get请求连接
        HttpGet httpGet=new HttpGet(uriBuilder.build());
        //设置请求头信息
        httpGet.addHeader("header","header");
        //设置传输类型
        httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded");
        //5.执行请求
        CloseableHttpResponse response=client.execute(httpGet);
        System.out.println(response.getEntity().getContentLength()+","+response.getStatusLine());
        // 配置信息
        RequestConfig requestConfig=RequestConfig.custom()
                .setConnectTimeout(5000) //设置连接超时时间，单位毫秒
                .setConnectionRequestTimeout(5000) //设置请求超时时间，单位毫秒
                .setSocketTimeout(5000)  //设置读写超时时间
                .setRedirectsEnabled(true)//设置是否重定向，默认为true
                .build();
        //将配置信息 添加到get请求中
        httpGet.setConfig(requestConfig);
        //根据响应码判断是否访问成功，如果是200则访问接口成功
        if (response.getStatusLine().getStatusCode()==200){
           //6.获取响应的实体信息
           HttpEntity entity=response.getEntity();
            //把返回的结果转化成字符串显示
            String resultString=EntityUtils.toString(entity,"UTF-8");
            System.out.println(resultString);
        }else{
            System.out.println("访问失败!");
        }
        //释放连接
        response.close();
        client.close();
    }
}
